package com.dc.project.basics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dc.common.annotation.DataScope;
import com.dc.common.constant.CustomSqlConstants;
import com.dc.common.exception.ServiceException;
import com.dc.common.utils.PageUtil;
import com.dc.common.utils.StringUtil;
import com.dc.common.vo.ResultVo;
import com.dc.project.basics.dao.BasClienteleDao;
import com.dc.project.basics.dto.BasClienteleDto;
import com.dc.project.basics.entity.BasClientele;
import com.dc.project.basics.service.BasClienteleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;

/**
 * 客户表(BasClientele)表服务实现类
 *
 * @author zhuangcy
 * @since 2021-06-12 16:56:50
 */
@Service("basClienteleService")
public class BasClienteleServiceImpl extends ServiceImpl<BasClienteleDao, BasClientele> implements BasClienteleService {

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return
     */
    @Override
    public ResultVo queryById(Serializable id) {
        return ResultVo.success().data(this.getById(id));
    }

    /**
     * 分页查询客户表
     *
     * @param dto
     * @return
     */
    @DataScope
    @Override
    public ResultVo queryByPage(BasClienteleDto dto) {
        QueryWrapper<BasClientele> queryWrapper = new QueryWrapper<>();
        IPage<BasClientele> page = this.page(PageUtil.convert2PageParam(dto), queryWrapper);
        return ResultVo.success().data(PageUtil.convert2PageResult(page));
    }

    /**
     * 新增客户表
     *
     * @param basClientele
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo add(BasClientele basClientele) {
        LambdaQueryWrapper<BasClientele> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BasClientele::getClienteleCode, basClientele.getClienteleCode())
                .last(CustomSqlConstants.LIMIT_1);
        BasClientele one = this.getOne(queryWrapper);
        if (null != one) {
            throw new ServiceException(StringUtil.format("{}该编码已存在", basClientele.getClienteleCode()));
        }
        return ResultVo.verify(this.save(basClientele));
    }

    /**
     * 编辑客户表
     *
     * @param basClientele
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo edit(BasClientele basClientele) {
        LambdaQueryWrapper<BasClientele> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BasClientele::getClienteleCode, basClientele.getClienteleCode())
                .last(CustomSqlConstants.LIMIT_1);
        BasClientele one = this.getOne(queryWrapper);
        if (null != one && !one.getClienteleId().equals(basClientele.getClienteleId())) {
            throw new ServiceException(StringUtil.format("{}该编码已存在", basClientele.getClienteleCode()));
        }
        return ResultVo.verify(this.updateById(basClientele));
    }

    /**
     * 根据主键删除客户表
     *
     * @param id 主键
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo delete(Serializable id) {
        return ResultVo.verify(this.removeById(id));
    }
}